package com.lk;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class JsonConvert {

	static JSONObject dataJson =null;
	BufferedWriter bw =null;
	BufferedReader br=null;
	public void initInputStream(String path){
		try {
			FileInputStream	fileInputStream = new FileInputStream(path);
			InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
			br = new BufferedReader(inputStreamReader);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	public void initOutputStream(String path){
		try {
			FileOutputStream fileOutputStream = new FileOutputStream(path);
			OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
			bw = new BufferedWriter(outputStreamWriter);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void init(){
		try {	
			String s = "", ws = null, stemp=null;
			while((stemp = br.readLine()) != null){
				s += stemp;
			}
			dataJson = new JSONObject(s);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{			
			try {
				br.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}  	
	}
/*	public void selectAll(JSONObject jsonObject) throws JSONException{
		Iterator it = jsonObject.keys();  
		while(it.hasNext()){
			String key =it.next().toString();
			System.out.println(key);
			JSONArray array =jsonObject.optJSONArray(key) ;
			JSONObject object =jsonObject.optJSONObject(key);
			if(array != null){
				for (int i = 0; i < array.length(); i++) {
					JSONObject object2 =array.optJSONObject(i);
					if(array.optJSONObject(i)!=null){
						selectAll(object2);
					}else{
						//System.out.println(array.get(i).toString());
					}

				}
			}
			else if(object!=null){
				selectAll(object);
				//selectAll(object);
			} 
			else{
				//System.out.println(jsonObject.get(key).toString());
			}    	
		}
	}*/
	public Object selectByKey(String keyname,JSONObject jsonObject) throws JSONException{

		Iterator it = jsonObject.keys();  
		while(it.hasNext()){
			String key =it.next().toString();
			JSONArray array =jsonObject.optJSONArray(key);
			JSONObject object =jsonObject.optJSONObject(key);
			if(key.equals(keyname)){
				System.out.println(key);
	             if(array!=null){
	            	 return array;
	             }else if(object!=null){
	            	 return object;
	             }else{
	            	 return jsonObject.get(key).toString();
	             }
			}
			if(array != null){
				for (int i = 0; i < array.length(); i++) {
					JSONObject object2 =array.optJSONObject(i);
					if(array.optJSONObject(i)!=null){
					  return	selectByKey(keyname,object2);
					}else{
					
						//System.out.println(array.get(i).toString());
					}

				}
			}
			else if(object!=null){

			return	selectByKey(keyname,object);
			//	selectAll(object);
			} 
			else{
			
				//System.out.println(dataJson.get(key).toString());
			}    	
		}
		return null;
	}
	/** 
	 * @param args 
	 * @throws JSONException 
	 */  
	public static void main(String[] args) throws JSONException {  
		JsonConvert jc=	new JsonConvert();
		jc.initInputStream("e:\\json3.json");
		jc.init();
		//jc.selectAll(dataJson);
        System.out.println(jc.selectByKey("name",dataJson).toString());
		/* try {  
        	FileInputStream fileInputStream = new FileInputStream("e:\\json2.json");
    		InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
        	FileOutputStream fileOutputStream = new FileOutputStream("e:\\json3.json");
    		OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
    	    BufferedWriter bw = new BufferedWriter(outputStreamWriter);
    		BufferedReader br = new BufferedReader(inputStreamReader);
            String s = "", ws = null, stemp=null;
        	while((stemp = br.readLine()) != null){
        		s += stemp;
        		}
        	System.out.println(s);
            JSONObject dataJson = new JSONObject(s);// 创建一个包含原始json串的json对象  

            JSONArray features = dataJson.getJSONArray("features");// 找到features的json数组  
            for (int j = 0; j < features.length(); j++) {
            	JSONObject info = features.getJSONObject(j);// 获取features数组的第i个json对象  
                JSONObject properties = info.getJSONObject("properties");// 找到properties的json对象  
                String name = properties.getString("name");// 读取properties对象里的name字段值  
                System.out.println(name); 
                properties.remove("ISO");// 删除ISO字段  
                properties.append("iq", "little");
			}
            ws = dataJson.toString();  
            bw.write(ws);  
            bw.newLine();  
            bw.flush();  
            br.close();  
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } */ 

	}  

}  